﻿using Common;
using IP20.Procdure;
using IP20.View;
using IP20.ViewModel;
using NewDevice.USB;
using NewDevice.XiWei;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Data.OleDb;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;

namespace IP20.ViewModel
{
    internal class DebugViewModel : NotifyBase
    {
        public ObservableCollection<string> Params { get; set; }
        public ObservableCollection<bool> Station1Ch16DI { get; set; }
        public ObservableCollection<bool> Station1Ch16DO { get; set; }
        public ObservableCollection<bool> Station2Ch16DI { get; set; }
        public ObservableCollection<bool> Station2Ch16DO { get; set; }

        //public ObservableCollection<bool> Station1Ch8DI { get; set; }
        public ObservableCollection<bool> Station1Ch8DO { get; set; }

        //public ObservableCollection<bool> Station2Ch8DI { get; set; }
        public ObservableCollection<bool> Station2Ch8DO { get; set; }

        public ObservableCollection<XiweiFlashInfo> FlashInfos1 { get; set; } = new ObservableCollection<XiweiFlashInfo>();
        public ObservableCollection<XiweiFlashInfo> FlashInfos2 { get; set; } = new ObservableCollection<XiweiFlashInfo>();

        public CommandBase ButtonClickCommand { get; set; }

        private string _log;

        public string Log
        {
            get { return _log; }
            set
            {
                _log = value;
                this.DoNotify();
            }
        }

        private int _stationIndex;
        private bool refresh1;
        private bool refresh2;

        private XiweiFlashInfo _flash1;

        public XiweiFlashInfo Flash1
        {
            get { return _flash1; }
            set
            {
                _flash1 = value;
                this.DoNotify();
            }
        }

        private XiweiFlashInfo _flash2;

        public XiweiFlashInfo Flash2
        {
            get { return _flash2; }
            set
            {
                _flash2 = value;
                this.DoNotify();
            }
        }

        public int StationIndex
        {
            get { return _stationIndex; }
            set
            {
                _stationIndex = value;
                this.DoNotify();
            }
        }

        public DebugViewModel()
        {
            Station1Ch16DI = new();
            Station1Ch16DO = new();
            Station2Ch16DI = new();
            Station2Ch16DO = new();
            //Station1Ch8DI = new();
            Station1Ch8DO = new();
            //Station2Ch8DI = new();
            Station2Ch8DO = new();
            for (int i = 0; i < 16; i++)
            {
                Station1Ch16DI.Add(true);
                Station1Ch16DO.Add(true);
                Station2Ch16DI.Add(true);
                Station2Ch16DO.Add(true);
                if (i < 8)
                {
                    //Station1Ch8DI.Add(true);
                    Station1Ch8DO.Add(true);
                    //Station2Ch8DI.Add(true);
                    Station2Ch8DO.Add(true);
                }
            }

            Task.Run(() =>
            {
                while (true)
                {
                    try
                    {
                        refresh1 = true;
                        for (int i = 0; i < 16; i++)
                        {
                            Station1Ch16DI[i] = Global.List16ChPQW[0].GetDI(i);
                            Station1Ch16DO[i] = Global.List16ChPQW[0].GetDO(i);
                            if (i < 8)
                            {
                                //Station1Ch8DI[i] = Global.List8ChPQW[0].GetDI(i);
                                Station1Ch8DO[i] = Global.List8ChPQW[0].GetDO(i);
                            }
                        }

                        refresh1 = false;
                        Thread.Sleep(100);
                    }
                    catch (Exception ex)
                    {
                        Mylog.Error(ex);
                    }
                }
            });

            Task.Run(() =>
            {
                while (true)
                {
                    try
                    {
                        refresh2 = true;
                        for (int i = 0; i < 16; i++)
                        {
                            Station2Ch16DI[i] = Global.List16ChPQW[1].GetDI(i);
                            Station2Ch16DO[i] = Global.List16ChPQW[1].GetDO(i);
                            if (i < 8)
                            {
                                //Station2Ch8DI[i] = Global.List8ChPQW[1].GetDI(i);
                                Station2Ch8DO[i] = Global.List8ChPQW[1].GetDO(i);
                            }
                        }

                        refresh2 = false;
                        Thread.Sleep(100);
                    }
                    catch (Exception ex)
                    {
                        Mylog.Error(ex);
                    }
                }
            });
            Station1Ch16DO.CollectionChanged += Station1Ch16DO_CollectionChanged;
            Station1Ch8DO.CollectionChanged += Station1Ch8DO_CollectionChanged;
            Station2Ch16DO.CollectionChanged += Station2Ch16DO_CollectionChanged;
            Station2Ch8DO.CollectionChanged += Station2Ch8DO_CollectionChanged;

            ButtonClickCommand = new CommandBase(ButtonClick);
            Params = new ObservableCollection<string>();
            for (int i = 0; i < 180; i++)
            {
                Params.Add("1");
            }

            Params[50] = "恒流模式";
            Params[80] = Config.Params["SN长度"];
            Params[81] = Config.Params["测试配置路径"];
            Params[82] = Config.Params["测试报告路径"];
            Params[83] = Config.Params["工位1是否启用"];
            Params[84] = Config.Params["工位2是否启用"];
#if JQ8931314
            Params[100] = Global.Jq1314Config.GetParams("Jq89313工位1通道1电阻点检值");
            Params[101] = Global.Jq1314Config.GetParams("Jq89313工位1通道2电阻点检值");
            Params[102] = Global.Jq1314Config.GetParams("Jq89313工位1通道3电阻点检值");
            Params[103] = Global.Jq1314Config.GetParams("Jq89313工位1通道4电阻点检值");
            Params[104] = Global.Jq1314Config.GetParams("Jq89313工位1通道1电阻点检值下限");
            Params[105] = Global.Jq1314Config.GetParams("Jq89313工位1通道2电阻点检值下限");
            Params[106] = Global.Jq1314Config.GetParams("Jq89313工位1通道3电阻点检值下限");
            Params[107] = Global.Jq1314Config.GetParams("Jq89313工位1通道4电阻点检值下限");
            Params[108] = Global.Jq1314Config.GetParams("Jq89313工位1通道1电阻点检值上限");
            Params[109] = Global.Jq1314Config.GetParams("Jq89313工位1通道2电阻点检值上限");
            Params[110] = Global.Jq1314Config.GetParams("Jq89313工位1通道3电阻点检值上限");
            Params[111] = Global.Jq1314Config.GetParams("Jq89313工位1通道4电阻点检值上限");
            Params[112] = Global.Jq1314Config.GetParams("Jq89313工位2通道1电阻点检值");
            Params[113] = Global.Jq1314Config.GetParams("Jq89313工位2通道2电阻点检值");
            Params[114] = Global.Jq1314Config.GetParams("Jq89313工位2通道3电阻点检值");
            Params[115] = Global.Jq1314Config.GetParams("Jq89313工位2通道4电阻点检值");
            Params[116] = Global.Jq1314Config.GetParams("Jq89313工位2通道1电阻点检值下限");
            Params[117] = Global.Jq1314Config.GetParams("Jq89313工位2通道2电阻点检值下限");
            Params[118] = Global.Jq1314Config.GetParams("Jq89313工位2通道3电阻点检值下限");
            Params[119] = Global.Jq1314Config.GetParams("Jq89313工位2通道4电阻点检值下限");
            Params[120] = Global.Jq1314Config.GetParams("Jq89313工位2通道1电阻点检值上限");
            Params[121] = Global.Jq1314Config.GetParams("Jq89313工位2通道2电阻点检值上限");
            Params[122] = Global.Jq1314Config.GetParams("Jq89313工位2通道3电阻点检值上限");
            Params[123] = Global.Jq1314Config.GetParams("Jq89313工位2通道4电阻点检值上限");
            Params[124] = Global.Jq1314Config.GetParams("Jq89314工位1通道1电压点检值");
            Params[125] = Global.Jq1314Config.GetParams("Jq89314工位1通道2电压点检值");
            Params[126] = Global.Jq1314Config.GetParams("Jq89314工位1通道3电压点检值");
            Params[127] = Global.Jq1314Config.GetParams("Jq89314工位1通道4电压点检值");
            Params[128] = Global.Jq1314Config.GetParams("Jq89314工位1通道1电阻点检值");
            Params[129] = Global.Jq1314Config.GetParams("Jq89314工位1通道2电阻点检值");
            Params[130] = Global.Jq1314Config.GetParams("Jq89314工位1通道3电阻点检值");
            Params[131] = Global.Jq1314Config.GetParams("Jq89314工位1通道4电阻点检值");
            Params[132] = Global.Jq1314Config.GetParams("Jq89314工位1通道5电阻点检值");
            Params[133] = Global.Jq1314Config.GetParams("Jq89314工位1通道1电压点检值下限");
            Params[134] = Global.Jq1314Config.GetParams("Jq89314工位1通道2电压点检值下限");
            Params[135] = Global.Jq1314Config.GetParams("Jq89314工位1通道3电压点检值下限");
            Params[136] = Global.Jq1314Config.GetParams("Jq89314工位1通道4电压点检值下限");
            Params[137] = Global.Jq1314Config.GetParams("Jq89314工位1通道1电压点检值上限");
            Params[138] = Global.Jq1314Config.GetParams("Jq89314工位1通道2电压点检值上限");
            Params[139] = Global.Jq1314Config.GetParams("Jq89314工位1通道3电压点检值上限");
            Params[140] = Global.Jq1314Config.GetParams("Jq89314工位1通道4电压点检值上限");
            Params[141] = Global.Jq1314Config.GetParams("Jq89314工位1通道4电阻点检值下限");
            Params[142] = Global.Jq1314Config.GetParams("Jq89314工位1通道4电阻点检值上限");
            Params[143] = Global.Jq1314Config.GetParams("Jq89314工位2通道1电压点检值");
            Params[144] = Global.Jq1314Config.GetParams("Jq89314工位2通道2电压点检值");
            Params[145] = Global.Jq1314Config.GetParams("Jq89314工位2通道3电压点检值");
            Params[146] = Global.Jq1314Config.GetParams("Jq89314工位2通道4电压点检值");
            Params[147] = Global.Jq1314Config.GetParams("Jq89314工位2通道1电阻点检值");
            Params[148] = Global.Jq1314Config.GetParams("Jq89314工位2通道2电阻点检值");
            Params[149] = Global.Jq1314Config.GetParams("Jq89314工位2通道3电阻点检值");
            Params[150] = Global.Jq1314Config.GetParams("Jq89314工位2通道4电阻点检值");
            Params[151] = Global.Jq1314Config.GetParams("Jq89314工位2通道5电阻点检值");
            Params[152] = Global.Jq1314Config.GetParams("Jq89314工位2通道1电压点检值下限");
            Params[153] = Global.Jq1314Config.GetParams("Jq89314工位2通道2电压点检值下限");
            Params[154] = Global.Jq1314Config.GetParams("Jq89314工位2通道3电压点检值下限");
            Params[155] = Global.Jq1314Config.GetParams("Jq89314工位2通道4电压点检值下限");
            Params[156] = Global.Jq1314Config.GetParams("Jq89314工位2通道1电压点检值上限");
            Params[157] = Global.Jq1314Config.GetParams("Jq89314工位2通道2电压点检值上限");
            Params[158] = Global.Jq1314Config.GetParams("Jq89314工位2通道3电压点检值上限");
            Params[159] = Global.Jq1314Config.GetParams("Jq89314工位2通道4电压点检值上限");
            Params[160] = Global.Jq1314Config.GetParams("Jq89314工位2通道4电阻点检值下限");
            Params[161] = Global.Jq1314Config.GetParams("Jq89314工位2通道4电阻点检值上限");

#endif
        }

        private void Station2Ch8DO_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (!refresh2)
            {
                int index = e.NewStartingIndex;
                bool value = (sender as ObservableCollection<bool>)[index];
                Global.List8ChPQW[1].SetDO(index, value);
            }
        }

        private void Station2Ch16DO_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (!refresh2)
            {
                int index = e.NewStartingIndex;
                bool value = (sender as ObservableCollection<bool>)[index];
                Global.List16ChPQW[1].SetDO(index, value);
            }
        }

        private void Station1Ch8DO_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (!refresh1)
            {
                int index = e.NewStartingIndex;
                bool value = (sender as ObservableCollection<bool>)[index];
                Global.List8ChPQW[0].SetDO(index, value);
            }
        }

        private void Station1Ch16DO_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (!refresh1)
            {
                int index = e.NewStartingIndex;
                bool value = (bool)e.NewItems[0];
                Global.List16ChPQW[0].SetDO(index, value);
            }
        }

        private void ButtonClick(object obj)
        {
            Task.Run(() =>
            {
                if (Global.IsBusy)
                {
                    Log = "正在执行其他操作,请完成后再尝试!";
                    return;
                }
                Global.IsBusy = true;
                try
                {
                    int cmd = int.Parse(obj.ToString());
                    string message = "";
                    switch (cmd)
                    {
                        case 0:
                            Global.ListIT6302[StationIndex].SetOutputStatus(false);
                            break;

                        case 1:
                            Global.ListIT6302[StationIndex].SetOutputStatus(true);
                            break;

                        case 2:
                            for (int i = 1; i <= 3; i++)
                            {
                                double vol = Global.ListIT6302[StationIndex].GetVoltage(i);
                                double cur = Global.ListIT6302[StationIndex].GetCurrent(i);

                                message += $"通道{i} 电压={vol}V 电流={cur}A ";
                            }
                            break;

                        case 3:
                            for (int i = 1; i <= 3; i++)
                            {
                                Global.ListIT6302[StationIndex].SetVoltage(i, double.Parse(Params[0 + (i - 1) * 2]));
                                Global.ListIT6302[StationIndex].SetCurrent(i, double.Parse(Params[1 + (i - 1) * 2]));
                            }
                            break;

                        case 4:
                            for (int i = 1; i <= 3; i++)
                            {
                                double vol = Global.ListIT6302[StationIndex].MeasVoltage(i);
                                double cur = Global.ListIT6302[StationIndex].MeasCurrent(i);

                                double errorDiffVol = double.Parse(TestModule.GetParams($"工位{StationIndex + 1}IT6302通道{i}电压读值误差"));   //工位1IT6302通道1电压读值误差
                                double errorDiffCur = double.Parse(TestModule.GetParams($"工位{StationIndex + 1}IT6302通道{i}电流读值误差"));   //工位1IT6302通道1电流读值误差

                                message += $"通道{i} 测量电压={vol + errorDiffVol}V 测量电流={cur + errorDiffCur}A ";
                            }
                            break;

                        case 5:
                            message = Global.ListIT6302[StationIndex].GetIDN();
                            break;

                        case 10:
                            Global.ListRUPower[StationIndex].SetStatus(false);
                            break;

                        case 11:
                            Global.ListRUPower[StationIndex].SetStatus(true);
                            break;

                        case 12:
                            {
                                double vol = Global.ListRUPower[StationIndex].GetVoltage();
                                double cur = Global.ListRUPower[StationIndex].GetCurrent();

                                message = $"电压={vol}V 电流={cur}A ";
                            }
                            break;

                        case 13:
                            Global.ListRUPower[StationIndex].SetVoltage(double.Parse(Params[10]));
                            Global.ListRUPower[StationIndex].SetCurrent(double.Parse(Params[11]));

                            break;

                        case 14:
                            {
                                double vol = Global.ListRUPower[StationIndex].MeasVoltage();
                                double cur = Global.ListRUPower[StationIndex].MeasCurrent();
                                var valueDiffVol = double.Parse(TestModule.GetParams($"工位{StationIndex + 1}RU-12-3040电压读值误差")); //工位1RU-12-3040电压读值误差
                                var valueDiffCur = double.Parse(TestModule.GetParams($"工位{StationIndex + 1}RU-12-3040电流读值误差")); //工位1RU-12-3040电压读值误差

                                message = $"测量电压={vol + valueDiffVol}V 测量电流={cur + valueDiffCur}A ";
                            }
                            break;

                        case 30:
                            {
                                var ch = int.Parse(Params[31]);

                                int devIndexOffset = StationIndex == 1 ? 3 : 0;

                                List<double> values = new List<double>();
                                for (int i = 0; i < 3; i++)
                                {
                                    var value = Global.ListVoltageCards[i + devIndexOffset].GetValue();
                                    values.AddRange(value);
                                    Thread.Sleep(10);
                                }

                                List<double> errorDiff = new List<double>();
                                for (int i = 0; i < 19; i++)
                                {
                                    errorDiff.Add(double.Parse(TestModule.GetParams($"工位{StationIndex + 1}电压采集卡通道{i + 1}读值误差")));//工位1电压采集卡通道1读值误差
                                }
                                message = $"通道{ch}电压={values[ch - 1] + errorDiff[ch - 1]}";
                            }
                            break;

                        case 31:
                            {
                                int devIndexOffset = StationIndex == 1 ? 3 : 0;

                                List<double> values = new List<double>();
                                for (int i = 0; i < 3; i++)
                                {
                                    var value = Global.ListVoltageCards[i + devIndexOffset].GetValue();
                                    values.AddRange(value);
                                    Thread.Sleep(10);
                                }

                                for (int i = 0; i < values.Count; i++)
                                {
                                    message += $"[ch{i + 1}]\t";
                                }
                                message += "\n";

                                List<double> errorDiff = new List<double>();
                                for (int i = 0; i < 19; i++)
                                {
                                    errorDiff.Add(double.Parse(TestModule.GetParams($"工位{StationIndex + 1}电压采集卡通道{i + 1}读值误差")));//工位1电压采集卡通道1读值误差
                                }

                                for (int i = 0; i < values.Count; i++)
                                {
                                    message += $"{values[i] + errorDiff[i]}\t";
                                }
                            }
                            break;

                        case 40:
                            {
                                var ch = int.Parse(Params[41]);

                                int devIndexOffset = StationIndex == 1 ? 5 : 0;

                                List<double> values = new List<double>();
                                for (int i = 0; i < 3; i++)
                                {
                                    var value = Global.ListCurrentCards[i + devIndexOffset].GetValue();
                                    values.AddRange(value);
                                    Thread.Sleep(10);
                                }

                                List<double> errorDiff = new List<double>();
                                for (int i = 0; i < 19; i++)
                                {
                                    errorDiff.Add(double.Parse(TestModule.GetParams($"工位{StationIndex + 1}电流采集卡通道{i + 1}读值误差")));//工位1电压采集卡通道1读值误差
                                }
                                var cur = Math.Round(values[ch - 1] / 249 * 1000 + errorDiff[ch - 1], 3);

                                message = $"通道{ch}电流={cur}";
                            }
                            break;

                        case 41:
                            {
                                var ch = int.Parse(Params[41]);

                                int devIndexOffset = StationIndex == 1 ? 5 : 0;

                                List<double> values = new List<double>();
                                for (int i = 0; i < 3; i++)
                                {
                                    var value = Global.ListCurrentCards[i + devIndexOffset].GetValue();
                                    values.AddRange(value);
                                }
                                for (int i = 0; i < values.Count; i++)
                                {
                                    message += $"[ch{i + 1}]\t";
                                }
                                message += "\n";

                                List<double> errorDiff = new List<double>();
                                for (int i = 0; i < 19; i++)
                                {
                                    errorDiff.Add(double.Parse(TestModule.GetParams($"工位{StationIndex + 1}电流采集卡通道{i + 1}读值误差")));//工位1电压采集卡通道1读值误差
                                }

                                for (int i = 0; i < values.Count; i++)
                                {
                                    var cur = Math.Round(values[i] / 249 * 1000 + errorDiff[i], 3);
                                    message += $"{cur}\t";
                                }
                            }
                            break;

                        case 42:
                            {
                                var ch = int.Parse(Params[42]);

                                int devIndexOffset = StationIndex == 1 ? 5 : 0;

                                List<double> values = new List<double>();
                                for (int i = 3; i < 5; i++)
                                {
                                    var value = Global.ListCurrentCards[i + devIndexOffset].GetValue();
                                    values.AddRange(value);
                                    Thread.Sleep(10);
                                }

                                List<double> errorDiff = new List<double>();
                                for (int i = 0; i < 16; i++)
                                {
                                    errorDiff.Add(double.Parse(TestModule.GetParams($"工位{StationIndex + 1}电流采集卡(NPN)通道{i + 1}读值误差")));//工位1电压采集卡通道1读值误差
                                }
                                var cur = Math.Round(values[ch - 1] / 249 * 1000 + errorDiff[ch - 1], 3);

                                message = $"通道{ch}电流={cur}";
                            }
                            break;

                        case 43:
                            {
                                var ch = int.Parse(Params[42]);

                                int devIndexOffset = StationIndex == 1 ? 5 : 0;

                                List<double> values = new List<double>();
                                for (int i = 3; i < 5; i++)
                                {
                                    var value = Global.ListCurrentCards[i + devIndexOffset].GetValue();
                                    values.AddRange(value);
                                }
                                for (int i = 0; i < values.Count; i++)
                                {
                                    message += $"[ch{i + 1}]\t";
                                }
                                message += "\n";

                                List<double> errorDiff = new List<double>();
                                for (int i = 0; i < 16; i++)
                                {
                                    errorDiff.Add(double.Parse(TestModule.GetParams($"工位{StationIndex + 1}电流采集卡(NPN)通道{i + 1}读值误差")));//工位1电压采集卡通道1读值误差
                                }

                                for (int i = 0; i < values.Count; i++)
                                {
                                    var cur = Math.Round(values[i] / 249 * 1000 + errorDiff[i], 3);
                                    message += $"{cur}\t";
                                }
                            }
                            break;

                        case 50:
                            {
                                Global.ListDL3021A[StationIndex].SetOutputStatus(false);
                            }
                            break;

                        case 51:
                            {
                                Global.ListDL3021A[StationIndex].SetOutputStatus(true);
                            }
                            break;

                        case 52:
                            {
                                var mode = Global.ListDL3021A[StationIndex].GetMode();
                                message = "负载模式为:" + Enum.GetName(typeof(DL3021A_Mode), mode);
                            }
                            break;

                        case 53:
                            {
                                switch (Params[50])
                                {
                                    case "恒压模式":
                                        Global.ListDL3021A[StationIndex].SetMode(DL3021A_Mode.CV);
                                        break;

                                    case "恒流模式":
                                        Global.ListDL3021A[StationIndex].SetMode(DL3021A_Mode.CC);
                                        break;

                                    case "恒阻值模式":
                                        Global.ListDL3021A[StationIndex].SetMode(DL3021A_Mode.CR);
                                        break;

                                    case "恒功率模式":
                                        Global.ListDL3021A[StationIndex].SetMode(DL3021A_Mode.CP);
                                        break;

                                    default:
                                        break;
                                }
                            }
                            break;

                        case 54:
                            {
                                var vol = Global.ListDL3021A[StationIndex].MeasVoltage();
                                message = $"电压={vol}V";
                            }
                            break;

                        case 55:
                            {
                                var vol = double.Parse(Params[51]);
                                if (vol <= (int)DL3021A_Range.CV_15V)
                                {
                                    Global.ListDL3021A[StationIndex].SetRang(DL3021A_Mode.CV, DL3021A_Range.CV_15V);
                                }
                                else if (vol <= (int)DL3021A_Range.CV_150V)
                                {
                                    Global.ListDL3021A[StationIndex].SetRang(DL3021A_Mode.CV, DL3021A_Range.CV_150V);
                                }
                                else
                                {
                                    throw new Exception("超出最大量程");
                                }
                                Global.ListDL3021A[StationIndex].SetVoltageCV(vol);
                            }
                            break;

                        case 56:
                            {
                                var cur = Global.ListDL3021A[StationIndex].MeasCurrent();
                                message = $"电流={cur}A";
                            }
                            break;

                        case 57:
                            {
                                var cur = double.Parse(Params[52]);
                                if (cur <= (int)DL3021A_Range.CC_4A)
                                {
                                    Global.ListDL3021A[StationIndex].SetRang(DL3021A_Mode.CC, DL3021A_Range.CC_4A);
                                }
                                else if (cur <= (int)DL3021A_Range.CC_40A)
                                {
                                    Global.ListDL3021A[StationIndex].SetRang(DL3021A_Mode.CC, DL3021A_Range.CC_40A);
                                }
                                else
                                {
                                    throw new Exception("超出最大量程");
                                }
                                Global.ListDL3021A[StationIndex].SetCurrentCC(cur);
                            }
                            break;

                        case 59:
                            {
                                var res = double.Parse(Params[53]);
                                if (res <= (int)DL3021A_Range.CR_15)
                                {
                                    Global.ListDL3021A[StationIndex].SetRang(DL3021A_Mode.CR, DL3021A_Range.CR_15);
                                }
                                else if (res <= (int)DL3021A_Range.CR_15000)
                                {
                                    Global.ListDL3021A[StationIndex].SetRang(DL3021A_Mode.CR, DL3021A_Range.CR_15000);
                                }
                                else
                                {
                                    throw new Exception("超出最大量程");
                                }
                                Global.ListDL3021A[StationIndex].SetResCR(res);
                            }
                            break;

                        case 58:
                            {
                                var pow = Global.ListDL3021A[StationIndex].MeasPow();
                                message = $"={pow}W";
                            }

                            break;

                        case 60:
                            {
                                var pow = double.Parse(Params[54]);
                                Global.ListDL3021A[StationIndex].SetPowCP(pow);
                            }
                            break;

                        case 70:
                            {
                                int ch = int.Parse(Params[70]);
                                var col = Global.JX1000.ReadH(ch);
                                message = $"色相:{col}";
                                Params[71] = col.ToString();
                            }
                            break;

                        case 71:
                            {
                                int ch = int.Parse(Params[70]);
                                var lightness = Global.JX1000.ReadH(ch, JX1000_Type.Light);
                                message = $"亮度:{lightness}";
                            }
                            break;

                        case 72:
                            {
                                int ch = int.Parse(Params[70]);
                                var freq = Global.JX1000.ReadH(ch, JX1000_Type.Frequency);
                                message = $"频率:{freq}";
                            }
                            break;

                        case 80:
                            OpenFileDialog openFileDialog = new OpenFileDialog
                            {
                                Filter = "Excel Files (*.xlsx)|*.xlsx",
                                Title = "选择一个Excel文件",
                            };
                            if (openFileDialog.ShowDialog() == true)
                            {
                                string selectedFilePath = openFileDialog.FileName;
                                Params[81] = selectedFilePath;
                            }
                            break;

                        case 81:

                            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
                                {
                                    folderBrowserDialog.Description = "请选择一个文件夹";
                                    folderBrowserDialog.ShowNewFolderButton = true;

                                    if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                                    {
                                        string selectedPath = folderBrowserDialog.SelectedPath;
                                        Params[82] = selectedPath;
                                    }
                                }
                            }));

                            break;

                        case 82:
                            // 保存配置
                            if (Params[83] == "0" && Params[84] == "0")
                            {
                                throw new Exception("请确保至少打开一个工位");
                            }
                            string path = Params[81];
#if JQ8931314
                            if (!path.Contains("jq89313") && !path.Contains("jq89314"))
                            {
                                throw new Exception("此软件只能测试jq89313|jq89314机种");
                            }
#else
                            if (path.Contains("jq89313") || path.Contains("jq89314"))
                                                        {
                                                        throw new Exception("此软件不支持jq89313|jq89314机种");
                                                        }

#endif
                            Config.Params["SN长度"] = Params[80];
                            Config.Params["测试配置路径"] = Params[81];
                            Config.Params["测试报告路径"] = Params[82];
                            Config.Params["工位1是否启用"] = Params[83];
                            Config.Params["工位2是否启用"] = Params[84];
                            Config.SaveParam();
                            Global.IsConfigChanged = true;
                            message = "保存成功";
                            break;

                        case 92:
                            {
                                List<double> values = Global.TempCard.GetValue();
                                for (int i = 0; i < 3; i++)
                                {
                                    message += $"[ch{i + 1}]\t";
                                }

                                message += "\n";
                                for (int i = 0; i < 3; i++)
                                {
                                    message += $"{values[i]}℃\t";
                                }
                            }
                            break;

                        case 100: //查询烧录文件 工位1
                            {
                                Global.ListXiWeiFlash[0].GetFlashCount();
                                Global.ListXiWeiFlash[0].GetAllFlashInfo();
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    FlashInfos1.Clear();
                                    foreach (var flash in Global.ListXiWeiFlash[0].FlashInfos.OrderBy(x => x.FlashNo))
                                    {
                                        FlashInfos1.Add(flash);
                                    }
                                });
                            }
                            break;

                        case 101: //下载 工位1
                            {
                                throw new NotImplementedException();
                            }
                        case 102: //烧录 工位1
                            {
                                Global.ListXiWeiFlash[0].SelectFlash(Flash1.FlashNo);
                                Global.ListXiWeiFlash[0].StartFlash();
                            }
                            break;

                        case 103: //删除 工位1
                            {
                                throw new NotImplementedException();
                            }

                        case 110: //查询烧录文件 工位1
                            {
                                Global.ListXiWeiFlash[1].GetFlashCount();
                                Global.ListXiWeiFlash[1].GetAllFlashInfo();
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    FlashInfos2.Clear();
                                    foreach (var flash in Global.ListXiWeiFlash[1].FlashInfos.OrderBy(x => x.FlashNo))
                                    {
                                        FlashInfos2.Add(flash);
                                    }
                                });
                            }
                            break;

                        case 111: //下载 工位1
                            {
                                throw new NotImplementedException();
                            }
                        case 112: //烧录 工位1
                            {
                                Global.ListXiWeiFlash[1].SelectFlash(Flash2.FlashNo);
                                Global.ListXiWeiFlash[1].StartFlash();
                            }
                            break;

                        case 113: //删除 工位1
                            {
                                throw new NotImplementedException();
                            }

                        case 120: //Jq89313点检
                            {
                                // 点检

                                for (int ch = 0; ch < 4; ch++)
                                {
                                    double value = Convert.ToDouble(Params[100 + ch]);
                                    var lmt = Jq89313.GetLimit(ch, value);
                                    Params[100 + ch + 4] = lmt.Item1.ToString("0.000");
                                    Params[100 + ch + 8] = lmt.Item2.ToString("0.000");
                                }
                                for (int ch = 0; ch < 4; ch++)
                                {
                                    double value = Convert.ToDouble(Params[112 + ch]);
                                    var lmt = Jq89313.GetLimit(ch, value);
                                    Params[112 + ch + 4] = lmt.Item1.ToString("0.000");
                                    Params[112 + ch + 8] = lmt.Item2.ToString("0.000");
                                }

                                message = "点检成功";
                            }
                            break;

                        case 121: //Jq89313保存
                            {
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    if (MyMessageBox.Show("提示", "是否保存机种JQ89313点检值?") == true)
                                    {
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道1电阻点检值", Params[100]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道2电阻点检值", Params[101]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道3电阻点检值", Params[102]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道4电阻点检值", Params[103]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道1电阻点检值下限", Params[104]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道2电阻点检值下限", Params[105]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道3电阻点检值下限", Params[106]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道4电阻点检值下限", Params[107]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道1电阻点检值上限", Params[108]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道2电阻点检值上限", Params[109]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道3电阻点检值上限", Params[110]);
                                        Global.Jq1314Config.SetParams("Jq89313工位1通道4电阻点检值上限", Params[111]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道1电阻点检值", Params[112]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道2电阻点检值", Params[113]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道3电阻点检值", Params[114]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道4电阻点检值", Params[115]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道1电阻点检值下限", Params[116]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道2电阻点检值下限", Params[117]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道3电阻点检值下限", Params[118]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道4电阻点检值下限", Params[119]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道1电阻点检值上限", Params[120]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道2电阻点检值上限", Params[121]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道3电阻点检值上限", Params[122]);
                                        Global.Jq1314Config.SetParams("Jq89313工位2通道4电阻点检值上限", Params[123]);

                                        Global.Jq1314Config.SaveToFile(Global.Jq1314ConfigFile, "config");

                                        message = "保存成功";
                                    }
                                });
                            }
                            break;

                        case 122: //Jq89314点检
                            {
                                // 工位1点检
                                string errMsg = "";
                                for (int ch = 0; ch < 4; ch++)
                                {
                                    try
                                    {
                                        double vol = Convert.ToDouble(Params[124 + ch]);
                                        //double res = Convert.ToDouble(Params[128 + ch]);
                                        var lmt = Jq89314.GetLimit(vol, 0);
                                        Params[133 + ch] = lmt.Item1.ToString();
                                        Params[137 + ch] = lmt.Item2.ToString();
                                    }
                                    catch (Exception e)
                                    {
                                        errMsg += $"工位1通道{ch + 1}:{e.Message}";
                                        throw new Exception(errMsg);
                                    }
                                }

                                try
                                {
                                    double res = Convert.ToDouble(Params[132]);
                                    var lmt = Jq89314.GetLimit(res);
                                    Params[141] = lmt.Item1.ToString();
                                    Params[142] = lmt.Item2.ToString();
                                }
                                catch (Exception e)
                                {
                                    errMsg += $"工位1电阻点检发生错误:{e.Message}";
                                    throw new Exception(errMsg);
                                }

                                // 工位2点检
                                for (int ch = 0; ch < 4; ch++)
                                {
                                    try
                                    {
                                        double vol = Convert.ToDouble(Params[143 + ch]);
                                        //double res = Convert.ToDouble(Params[147 + ch]);
                                        var lmt = Jq89314.GetLimit(vol, 0);
                                        Params[152 + ch] = lmt.Item1.ToString();
                                        Params[156 + ch] = lmt.Item2.ToString();
                                    }
                                    catch (Exception e)
                                    {
                                        errMsg += $"工位2通道{ch + 1}:{e.Message}\n";
                                        throw new Exception(errMsg);
                                    }
                                }

                                try
                                {
                                    double res = Convert.ToDouble(Params[151]);
                                    var lmt = Jq89314.GetLimit(res);
                                    Params[160] = lmt.Item1.ToString("0.000");
                                    Params[161] = lmt.Item2.ToString("0.000");
                                }
                                catch (Exception e)
                                {
                                    errMsg += $"工位2电阻点检发生错误:{e.Message}";
                                    throw new Exception(errMsg);
                                }

                                message = "点检成功";
                            }
                            break;

                        case 123: //Jq89314保存
                            {
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    if (MyMessageBox.Show("提示", "是否保存机种JQ89314点检值?") == true)
                                    {
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道1电压点检值", Params[124]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道2电压点检值", Params[125]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道3电压点检值", Params[126]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道4电压点检值", Params[127]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道1电阻点检值", Params[128]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道2电阻点检值", Params[129]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道3电阻点检值", Params[130]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道4电阻点检值", Params[131]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道5电阻点检值", Params[132]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道1电压点检值下限", Params[133]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道2电压点检值下限", Params[134]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道3电压点检值下限", Params[135]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道4电压点检值下限", Params[136]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道1电压点检值上限", Params[137]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道2电压点检值上限", Params[138]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道3电压点检值上限", Params[139]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道4电压点检值上限", Params[140]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道4电阻点检值下限", Params[141]);
                                        Global.Jq1314Config.SetParams("Jq89314工位1通道4电阻点检值上限", Params[142]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道1电压点检值", Params[143]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道2电压点检值", Params[144]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道3电压点检值", Params[145]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道4电压点检值", Params[146]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道1电阻点检值", Params[147]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道2电阻点检值", Params[148]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道3电阻点检值", Params[149]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道4电阻点检值", Params[150]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道5电阻点检值", Params[151]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道1电压点检值下限", Params[152]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道2电压点检值下限", Params[153]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道3电压点检值下限", Params[154]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道4电压点检值下限", Params[155]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道1电压点检值上限", Params[156]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道2电压点检值上限", Params[157]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道3电压点检值上限", Params[158]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道4电压点检值上限", Params[159]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道4电阻点检值下限", Params[160]);
                                        Global.Jq1314Config.SetParams("Jq89314工位2通道4电阻点检值上限", Params[161]);

                                        Global.Jq1314Config.SaveToFile(Global.Jq1314ConfigFile, "config");
                                        message = "保存成功";
                                    }
                                });
                            }
                            break;

                        default:
                            break;
                    }
                    Log = message;
                }
                catch (Exception ex)
                {
                    Log = ex.Message;
                }
                finally
                {
                    Global.IsBusy = false;
                }
            });
        }
    }

    public static class Jq89313
    {
        private const int AccuDev = 16;
        private static uint[] rtd_PT_table_k_p = [26014, 26910, 27859, 28862, 29923, 31045, 32237, 33504, 33504];

        private static uint[] rtd_PT_table_x_p = [10000, 14284, 18379, 22284, 25998, 29523, 32868, 36044, 39048, 40000];

        private static int[] rtd_PT_table_y_p = [0, 111454, 221647, 330442, 437619, 543091, 646939, 749321, 850000];//849982};

        private static uint[] rtd_PT_table_k_n = [25460, 25199, 24925, 24654, 24358, 24037, 23692, 23321, 23321];
        private static uint[] rtd_PT_table_x_n = [10000, 8723, 7508, 6456, 5470, 4525, 3611, 2716, 1852, 900];

        private static int[] rtd_PT_table_y_n = [0, -32517, -63129, -89361, -113654, -136686, -158659, -179853, -200000];//-200001};

        private static void Cal_Limit(int _Mag, double _Data, ref double dst)
        {
            double src = _Data * _Mag;
            double temp;
            if (src < 10000)
            {
                if (src <= rtd_PT_table_x_n[9])
                    dst = (rtd_PT_table_y_n[8] / 100) - 40;
                else
                {
                    for (int i = 0; i < 9; i++)
                    {
                        if (src > rtd_PT_table_x_n[i + 1])
                        {
                            temp = (rtd_PT_table_x_n[i] - src) * rtd_PT_table_k_n[i] / 1000;
                            dst = (rtd_PT_table_y_n[i] - (int)temp) / 100;
                            break;
                        }
                    }
                }
            }
            else
            {
                if (src >= rtd_PT_table_x_p[9])
                    dst = (rtd_PT_table_y_p[8] / 100) + 40;
                else
                {
                    for (int i = 0; i < 9; i++)
                    {
                        if (src < rtd_PT_table_x_p[i + 1])
                        {
                            temp = (src - rtd_PT_table_x_p[i]) * rtd_PT_table_k_p[i] / 1000;
                            dst = ((int)temp + rtd_PT_table_y_p[i]) / 100;
                            break;
                        }
                    }
                }
            }
        }

        private static double[] Resref_H = new double[4] { 1250, 1050, 205, 105 };
        private static double[] Resref_L = new double[4] { 1150, 950, 195, 95 };
        private static int[] Mag = new int[4] { 10, 10, 100, 100 };

        private static void Check_input_data(int channel, double value)
        {
            if ((value < Resref_L[channel]) || (value > Resref_H[channel]))
            {
                throw new Exception($"通道{channel + 1}电阻值不在范围区间[{Resref_L[channel]},{Resref_H[channel]}]");
            }
        }

        public static (double, double) GetLimit(int channel, double resistor)
        {
            double temp = 0;
            Check_input_data(channel, resistor);
            Cal_Limit(Mag[channel], resistor, ref temp);
            return (Math.Round(temp - AccuDev, 3), Math.Round(temp + AccuDev, 3));
        }
    }

    public static class Jq89314
    {
        #region 4通道电阻电压点检

        private static long[] type_e_voltage;
        private static long[] type_e_temperature;
        private static List<double> temperaturetable = new List<double>();
        private static List<double> resistortable = new List<double>();

        static Jq89314()
        {
            double tempInterval = 1.0;
            for (double init = -200.0; init <= 850.0;)
            {
                temperaturetable.Add(init);
                if (init > 0)
                {
                    resistortable.Add(1000.0 * (1 + (3.9083E-3 * init) + (-5.775E-7 * (Math.Pow(init, 2)))));
                }
                else
                {
                    resistortable.Add(1000.0 * (1 + (3.9083E-3 * init) + (-5.775E-7 * (Math.Pow(init, 2))) + ((-4.183E-12 * Math.Pow(init, 3)) * (init - 100.0))));
                }
                init = init + tempInterval;
            }
            type_e_voltage = new long[] {
           - 8825,-8820,-8815,-8810,-8805,-8801,-8796,-8791,-8786,-8781,-8776,-8772,-8767,-8762,-8757,-8752,
-8747,-8742,-8737,-8732,-8727,-8722,-8717,-8712,-8707,-8702,-8697,-8692,-8687,-8682,-8677,-8671,
-8666,-8661,-8655,-8650,-8645,-8639,-8634,-8629,-8623,-8618,-8612,-8607,-8601,-8596,-8591,-8585,
-8580,-8574,-8569,-8563,-8558,-8552,-8547,-8541,-8535,-8530,-8524,-8519,-8513,-8508,-8502,-8496,
-8491,-8485,-8479,-8474,-8468,-8462,-8457,-8451,-8445,-8440,-8434,-8428,-8422,-8415,-8409,-8403,
-8397,-8391,-8385,-8379,-8373,-8367,-8361,-8354,-8348,-8342,-8336,-8330,-8324,-8317,-8311,-8305,
-8299,-8293,-8286,-8280,-8274,-8267,-8261,-8255,-8249,-8242,-8236,-8230,-8223,-8217,-8210,-8204,
-8198,-8191,-8185,-8178,-8172,-8166,-8159,-8153,-8146,-8138,-8132,-8125,-8119,-8112,-8105,-8098,
-8092,-8085,-8077,-8071,-8064,-8057,-8050,-8042,-8036,-8029,-8022,-8015,-8007,-8000,-7994,-7987,
-7980,-7973,-7966,-7959,-7952,-7945,-7938,-7931,-7924,-7917,-7909,-7902,-7895,-7888,-7881,-7874,
-7866,-7859,-7852,-7845,-7837,-7830,-7823,-7815,-7808,-7800,-7792,-7785,-7777,-7769,-7762,-7754,
-7746,-7739,-7731,-7723,-7716,-7708,-7700,-7692,-7684,-7677,-7669,-7661,-7653,-7645,-7637,-7630,
-7622,-7614,-7606,-7598,-7590,-7582,-7574,-7566,-7558,-7550,-7542,-7534,-7526,-7518,-7510,-7502,
-7494,-7486,-7478,-7469,-7461,-7452,-7444,-7435,-7427,-7418,-7410,-7401,-7393,-7384,-7376,-7367,
-7359,-7350,-7341,-7333,-7324,-7315,-7307,-7298,-7289,-7281,-7272,-7263,-7255,-7246,-7237,-7228,
-7220,-7211,-7202,-7193,-7184,-7176,-7167,-7158,-7149,-7140,-7131,-7122,-7113,-7104,-7094,-7085,
-7076,-7066,-7057,-7048,-7038,-7029,-7019,-7010,-7001,-6991,-6982,-6972,-6963,-6953,-6944,-6934,
-6925,-6915,-6906,-6896,-6887,-6877,-6868,-6858,-6848,-6839,-6829,-6819,-6810,-6800,-6790,-6781,
-6771,-6761,-6751,-6741,-6731,-6721,-6711,-6700,-6690,-6680,-6670,-6660,-6649,-6639,-6629,-6619,
-6608,-6598,-6588,-6577,-6567,-6557,-6546,-6536,-6526,-6515,-6505,-6494,-6484,-6473,-6463,-6453,
-6442,-6432,-6421,-6411,-6400,-6389,-6379,-6368,-6358,-6347,-6336,-6325,-6314,-6303,-6292,-6280,
-6269,-6258,-6247,-6236,-6225,-6214,-6203,-6191,-6180,-6169,-6158,-6147,-6135,-6124,-6113,-6101,
-6090,-6079,-6068,-6056,-6045,-6033,-6022,-6011,-5999,-5988,-5976,-5965,-5954,-5942,-5931,-5919,
-5907,-5895,-5883,-5871,-5859,-5847,-5835,-5823,-5811,-5799,-5787,-5775,-5763,-5751,-5739,-5727,
-5715,-5702,-5690,-5678,-5666,-5654,-5641,-5629,-5617,-5605,-5592,-5580,-5568,-5556,-5543,-5531,
-5519,-5506,-5494,-5481,-5469,-5456,-5443,-5430,-5417,-5404,-5391,-5378,-5365,-5352,-5339,-5326,
-5313,-5300,-5287,-5274,-5261,-5248,-5235,-5222,-5209,-5196,-5182,-5169,-5156,-5143,-5130,-5116,
-5103,-5090,-5077,-5063,-5050,-5036,-5023,-5009,-4995,-4981,-4967,-4954,-4940,-4926,-4912,-4898,
-4884,-4870,-4856,-4842,-4828,-4814,-4800,-4786,-4772,-4758,-4744,-4730,-4716,-4702,-4688,-4674,
-4659,-4645,-4631,-4617,-4602,-4587,-4572,-4558,-4543,-4528,-4513,-4499,-4484,-4469,-4454,-4439,
-4425,-4410,-4395,-4380,-4365,-4350,-4335,-4320,-4305,-4290,-4275,-4260,-4245,-4230,-4215,-4199,
-4184,-4168,-4152,-4137,-4121,-4105,-4090,-4074,-4058,-4043,-4027,-4011,-3996,-3980,-3964,-3948,
-3932,-3917,-3901,-3885,-3869,-3853,-3837,-3820,-3804,-3787,-3771,-3754,-3738,-3721,-3705,-3688,
-3672,-3655,-3639,-3622,-3605,-3589,-3572,-3555,-3539,-3522,-3505,-3489,-3472,-3455,-3437,-3420,
-3403,-3385,-3368,-3351,-3333,-3316,-3299,-3281,-3264,-3246,-3229,-3211,-3194,-3176,-3159,-3141,
-3124,-3106,-3088,-3070,-3052,-3034,-3016,-2997,-2979,-2961,-2943,-2925,-2906,-2888,-2870,-2851,
-2833,-2815,-2796,-2778,-2759,-2741,-2722,-2704,-2685,-2666,-2647,-2628,-2609,-2590,-2571,-2552,
-2533,-2514,-2494,-2475,-2456,-2437,-2418,-2398,-2379,-2360,-2340,-2321,-2302,-2282,-2263,-2243,
-2223,-2203,-2183,-2163,-2143,-2123,-2103,-2083,-2063,-2043,-2021,-2001,-1982,-1962,-1942,-1921,
-1901,-1881,-1861,-1840,-1820,-1800,-1779,-1759,-1738,-1717,-1696,-1675,-1654,-1633,-1612,-1591,
-1570,-1548,-1527,-1506,-1485,-1464,-1443,-1421,-1400,-1379,-1358,-1337,-1315,-1294,-1273,-1251,
-1230,-1209,-1187,-1165,-1143,-1121,-1099,-1077,-1055,-1033,-1010,-989,-967,-944,-922,-900,
-878,-856,-833,-811,-789,-766,-743,-720,-697,-674,-651,-628,-605,-582,-558,-535,
-511,-487,-464,-439,-415,-391,-366,-342,-317,-292,-266,-240,-214,-188,-161,-134,
-106,-78,-49,-20,11,41,72,102,133,163,194,225,255,286,317,348,
378,409,440,471,501,532,562,592,623,653,684,714,745,775,806,836,
867,897,928,959,989,1020,1051,1082,1112,1143,1174,1205,1236,1267,1298,1328,
1359,1390,1421,1452,1484,1515,1546,1577,1608,1639,1670,1702,1733,1764,1795,1827,
1858,1890,1921,1952,1984,2015,2047,2078,2110,2141,2173,2205,2236,2268,2300,2331,
2363,2395,2427,2458,2490,2522,2554,2586,2618,2651,2684,2716,2749,2782,2814,2847,
2880,2912,2945,2978,3011,3044,3077,3110,3143,3176,3209,3242,3275,3308,3341,3374,
3407,3440,3474,3508,3542,3576,3610,3643,3677,3711,3745,3780,3814,3848,3882,3916,
3950,3984,4019,4053,4086,4122,4157,4192,4227,4262,4297,4332,4367,4403,4438,4473,
4508,4543,4579,4615,4651,4687,4723,4759,4795,4831,4867,4903,4939,4976,5012,5048,
5085,5122,5159,5196,5233,5270,5307,5344,5382,5419,5456,5494,5532,5570,5608,5646,
5684,5722,5760,5798,5836,5875,5914,5953,5992,6031,6070,6109,6148,6187,6226,6266,
6306,6346,6386,6426,6466,6506,6546,6586,6627,6668,6708,6749,6790,6831,6872,6913,
6955,6997,7038,7080,7122,7164,7206,7249,7291,7334,7376,7419,7462,7505,7548,7592,
7635,7679,7722,7767,7811,7855,7899,7944,7988,8032,8077,8124,8169,8214,8259,8305,
8351,8397,8443,8490,8536,8583,8630,8677,8724,8771,8819,8867,8914,8962,9010,9059,
9107,9156,9205,9254,9304,9353,9403,9452,9503,9553,9603,9654,9705,9756,9807,9859,
9911,9963,10015,10067,10120,10173,10225,10278,10332,10386,10440,10493,10548,10603,10657,10712,
10767,10823,10878,10935,10991,11047,11104,11161,11219,11276,11334,11392,11450,11509,11567,11626,
11686,11746,11805,11866,11926,11987,12048,12110,12172,12234,12296,12359,12422,12485,12549,12613,
12678,12742,12807,12873,12938,13005,13071,13138,13205,13273,13341,13409,13478,13546,13616,13686,
13756,13827,13898,13970,14042,14114,14187,14260,14334,14408,14483,14557,14633,14709,14785,14862,
14939,15017,15095,15174,15253,15333,15414,15494,15576,15658,15740,15823,15907,15991,16075,16161,
16247,16332,16420,16508,16596,16685,16775,16865,16956,17047,17139,17232,17326,17421,17516,17611,
17708,17805,17903,18002,18101,18202,18303,18405,18508,18612,18717,18822,18928,19036,19144,19252,
19363,19474,19586,19699,19813,19928,20045,20162,20280,20399,20520,20642,20765,20888,21014,21140,
21268,21397,21528,21660,21793,21928,22064,22202,22341,22482,22624,22768,22913,23061,23210,23361,
23514,23668,23825,23984,24145,24307,24472,24640,24810,24983,25158,25335,25515,25698,25884,26073,
26265,26461,26660,26863,27069,27279,27495,27714,27938,28168,28403,28643,28890,29143,29403,29671,
29948,30233,30530,30838,31158,31492,31843,32213,32604,33024,33478,33973,34527,35164,35941,37022,
41070,41969,42803,43500,44114,44673,45190,45676,46136,46575,46998,47406,47801,48186,48562,48930,
49290,49643,49992,50335,50674,51009,51341,51669,51995,52318,52638,52957,53274,53589,53904,54217,
54528,54839,55148,55458,55766,56074,56380,56685,56991,57295,57598,57901,58201,58501,58799,59096,
59391,59685,59975,60265,60551,60835,61117,61396,61672,61944,62214,62481,62744,63004,63260,63513,
63762,64007,64250,64489,64724,64955,65184,65409,65631,65849,66065,66277,66487,66693,66898,67099,
67297,67493,67687,67878,68067,68254,68440,68624,68805,68985,69164,69341,69516,69691,69865,70037,
70208,70380,70550,70719,70889,71058,71227,71396,71566,71736,71907,72079,72252,72427,72603,72782,
72964,73149,73339,73533,73733,73941,74159,74390,74638,74912,75225,75621,76373,};

            type_e_temperature = new long[] {
            -20000,-19981,-19962,-19943,-19924,-19905,-19886,-19867,-19848,-19829,-19810,-19791,-19772,-19753,-19734,-19715,
-19696,-19677,-19658,-19639,-19620,-19601,-19582,-19563,-19544,-19525,-19506,-19487,-19468,-19448,-19428,-19408,
-19388,-19368,-19348,-19328,-19308,-19288,-19268,-19248,-19228,-19208,-19188,-19168,-19148,-19128,-19108,-19088,
-19068,-19048,-19028,-19008,-18988,-18968,-18948,-18928,-18908,-18888,-18868,-18848,-18828,-18808,-18788,-18768,
-18748,-18728,-18708,-18688,-18668,-18648,-18628,-18608,-18588,-18568,-18547,-18526,-18505,-18484,-18463,-18442,
-18421,-18400,-18379,-18358,-18337,-18316,-18295,-18274,-18253,-18232,-18211,-18190,-18169,-18148,-18127,-18106,
-18085,-18064,-18043,-18022,-18001,-17980,-17959,-17938,-17917,-17896,-17875,-17854,-17833,-17812,-17791,-17770,
-17749,-17728,-17707,-17686,-17665,-17644,-17623,-17602,-17581,-17559,-17537,-17515,-17493,-17471,-17449,-17427,
-17405,-17383,-17361,-17339,-17317,-17295,-17273,-17251,-17229,-17207,-17185,-17163,-17141,-17119,-17097,-17075,
-17053,-17031,-17009,-16987,-16965,-16943,-16921,-16899,-16877,-16855,-16833,-16811,-16789,-16767,-16745,-16723,
-16701,-16679,-16657,-16635,-16613,-16591,-16569,-16546,-16523,-16500,-16477,-16454,-16431,-16408,-16385,-16362,
-16338,-16316,-16293,-16269,-16247,-16224,-16201,-16178,-16155,-16132,-16109,-16086,-16063,-16040,-16016,-15994,
-15971,-15947,-15925,-15902,-15879,-15856,-15833,-15810,-15787,-15763,-15741,-15718,-15694,-15672,-15649,-15626,
-15603,-15580,-15556,-15532,-15508,-15484,-15460,-15436,-15412,-15388,-15363,-15340,-15316,-15291,-15268,-15244,
-15219,-15196,-15172,-15147,-15124,-15100,-15076,-15052,-15028,-15004,-14980,-14956,-14932,-14908,-14884,-14860,
-14836,-14812,-14788,-14763,-14740,-14716,-14691,-14668,-14644,-14619,-14596,-14571,-14546,-14521,-14496,-14471,
-14446,-14421,-14396,-14371,-14346,-14321,-14296,-14271,-14246,-14221,-14196,-14171,-14146,-14121,-14096,-14071,
-14046,-14021,-13996,-13971,-13946,-13921,-13896,-13871,-13846,-13821,-13796,-13771,-13746,-13721,-13696,-13671,
-13646,-13621,-13594,-13569,-13543,-13516,-13491,-13465,-13438,-13413,-13387,-13361,-13335,-13309,-13283,-13257,
-13231,-13205,-13179,-13153,-13127,-13101,-13075,-13049,-13022,-12997,-12971,-12944,-12919,-12893,-12866,-12841,
-12815,-12789,-12763,-12737,-12711,-12685,-12659,-12633,-12607,-12580,-12553,-12526,-12499,-12472,-12445,-12418,
-12391,-12364,-12337,-12310,-12283,-12256,-12229,-12202,-12175,-12148,-12121,-12094,-12067,-12040,-12013,-11986,
-11959,-11932,-11905,-11878,-11851,-11824,-11797,-11770,-11743,-11716,-11689,-11662,-11635,-11608,-11581,-11554,
-11526,-11498,-11470,-11442,-11414,-11386,-11358,-11330,-11302,-11274,-11246,-11218,-11190,-11162,-11134,-11106,
-11078,-11050,-11022,-10994,-10966,-10938,-10910,-10882,-10854,-10826,-10798,-10770,-10742,-10714,-10686,-10658,
-10630,-10602,-10574,-10546,-10517,-10488,-10459,-10430,-10401,-10372,-10343,-10314,-10285,-10256,-10227,-10198,
-10169,-10140,-10111,-10082,-10053,-10024,-9995,-9966,-9937,-9908,-9879,-9850,-9821,-9792,-9763,-9734,
-9705,-9676,-9647,-9618,-9589,-9559,-9529,-9499,-9469,-9439,-9409,-9379,-9349,-9319,-9289,-9259,
-9229,-9199,-9169,-9139,-9109,-9079,-9049,-9019,-8989,-8959,-8929,-8899,-8869,-8839,-8809,-8779,
-8749,-8719,-8689,-8658,-8627,-8596,-8565,-8534,-8503,-8472,-8441,-8410,-8379,-8348,-8317,-8286,
-8255,-8224,-8193,-8162,-8131,-8100,-8069,-8038,-8006,-7976,-7945,-7914,-7883,-7852,-7820,-7788,
-7756,-7723,-7692,-7659,-7628,-7595,-7564,-7531,-7500,-7468,-7436,-7404,-7372,-7340,-7308,-7276,
-7244,-7212,-7180,-7148,-7116,-7084,-7051,-7018,-6984,-6952,-6919,-6886,-6853,-6820,-6787,-6754,
-6720,-6688,-6655,-6622,-6589,-6556,-6523,-6490,-6456,-6423,-6391,-6358,-6325,-6291,-6257,-6223,
-6189,-6155,-6121,-6087,-6053,-6019,-5985,-5951,-5917,-5883,-5849,-5815,-5781,-5747,-5713,-5679,
-5645,-5611,-5577,-5542,-5507,-5472,-5437,-5402,-5367,-5332,-5297,-5262,-5227,-5192,-5157,-5122,
-5087,-5052,-5017,-4982,-4947,-4912,-4877,-4842,-4807,-4771,-4735,-4699,-4663,-4627,-4591,-4555,
-4519,-4483,-4447,-4411,-4375,-4339,-4303,-4267,-4231,-4195,-4159,-4123,-4086,-4051,-4015,-3978,
-3940,-3904,-3867,-3829,-3793,-3756,-3719,-3682,-3645,-3608,-3571,-3534,-3497,-3460,-3422,-3386,
-3349,-3311,-3275,-3238,-3201,-3164,-3127,-3089,-3051,-3013,-2975,-2937,-2899,-2861,-2823,-2785,
-2747,-2709,-2671,-2633,-2595,-2557,-2519,-2481,-2443,-2405,-2367,-2329,-2291,-2253,-2215,-2177,
-2139,-2101,-2063,-2023,-1985,-1946,-1907,-1868,-1829,-1789,-1751,-1712,-1673,-1634,-1595,-1556,
-1517,-1478,-1439,-1400,-1361,-1321,-1281,-1241,-1201,-1161,-1121,-1081,-1041,-1001,-960,-919,
-877,-836,-796,-754,-712,-670,-628,-585,-542,-499,-455,-411,-366,-321,-275,-227,
-181,-133,-84,-34,18,70,122,174,225,278,330,382,434,486,538,590,
642,694,746,798,849,900,951,1002,1053,1104,1155,1206,1257,1308,1359,1410,
1461,1512,1563,1614,1664,1716,1767,1818,1869,1920,1971,2022,2073,2124,2175,2226,
2277,2328,2379,2430,2481,2532,2583,2634,2685,2736,2787,2838,2889,2940,2991,3042,
3093,3144,3195,3246,3297,3347,3399,3450,3501,3552,3603,3654,3704,3756,3807,3858,
3909,3960,4011,4061,4113,4164,4215,4266,4318,4370,4422,4474,4526,4578,4630,4682,
4734,4786,4838,4890,4942,4994,5046,5098,5150,5202,5254,5306,5358,5410,5462,5514,
5566,5618,5671,5724,5777,5830,5883,5936,5989,6042,6095,6148,6201,6254,6307,6360,
6413,6466,6519,6572,6625,6679,6733,6787,6841,6895,6948,7003,7056,7111,7165,7219,
7273,7327,7381,7436,7491,7545,7601,7656,7711,7766,7820,7876,7931,7986,8041,8095,
8152,8208,8264,8320,8376,8432,8488,8544,8600,8656,8712,8769,8826,8883,8940,8997,
9054,9111,9168,9225,9282,9340,9398,9456,9514,9572,9630,9688,9746,9804,9863,9922,
9981,10040,10099,10158,10217,10276,10335,10395,10455,10515,10575,10635,10695,10755,10815,10876,
10937,10998,11059,11120,11181,11242,11304,11366,11428,11490,11552,11614,11676,11739,11802,11865,
11928,11991,12054,12118,12182,12246,12310,12374,12438,12503,12568,12633,12698,12763,12828,12894,
12960,13026,13091,13158,13225,13291,13359,13426,13493,13560,13628,13696,13763,13832,13900,13969,
14038,14107,14176,14246,14316,14386,14456,14526,14597,14668,14738,14810,14882,14954,15026,15097,
15171,15244,15316,15390,15463,15538,15612,15686,15761,15836,15911,15986,16062,16138,16213,16290,
16366,16444,16521,16599,16677,16755,16834,16913,16992,17071,17151,17231,17311,17392,17473,17554,
17636,17718,17800,17883,17966,18049,18133,18217,18302,18387,18472,18558,18644,18730,18817,18904,
18992,19080,19168,19257,19346,19436,19526,19617,19708,19799,19891,19983,20076,20169,20263,20357,
20452,20547,20643,20739,20836,20933,21031,21129,21228,21327,21427,21527,21628,21729,21831,21934,
22037,22141,22245,22350,22455,22561,22668,22775,22883,22992,23101,23211,23322,23433,23545,23658,
23771,23885,24000,24116,24232,24349,24467,24586,24705,24825,24946,25068,25191,25315,25439,25564,
25689,25817,25945,26074,26204,26335,26467,26600,26733,26869,27005,27142,27280,27419,27558,27700,
27843,27987,28132,28277,28425,28574,28724,28875,29027,29181,29336,29493,29651,29810,29970,30133,
30297,30463,30630,30799,30970,31142,31316,31492,31670,31850,32031,32214,32399,32587,32777,32969,
33163,33359,33558,33759,33963,34169,34378,34590,34805,35023,35244,35468,35695,35926,36160,36398,
36640,36886,37136,37391,37650,37914,38184,38459,38740,39027,39321,39622,39930,40246,40571,40905,
41250,41606,41975,42358,42756,43172,43608,44067,44554,45074,45636,46250,46935,47724,48684,50021,
55021,56133,57164,58026,58787,59479,60120,60722,61292,61839,62364,62871,63363,63841,64310,64767,
65216,65658,66093,66522,66945,67364,67779,68190,68598,69003,69405,69805,70203,70599,70994,71388,
71780,72171,72561,72951,73340,73728,74115,74501,74887,75272,75656,76039,76420,76800,77178,77555,
77930,78303,78673,79041,79406,79768,80127,80483,80835,81183,81528,81869,82206,82538,82866,83190,
83510,83825,84136,84443,84745,85043,85337,85627,85913,86195,86473,86747,87018,87285,87549,87809,
88066,88320,88571,88819,89065,89308,89549,89788,90024,90258,90491,90722,90951,91179,91406,91631,
91855,92079,92302,92524,92746,92968,93190,93412,93635,93859,94084,94310,94538,94768,95001,95237,
95477,95722,95972,96229,96494,96770,97059,97365,97694,98057,98474,99000,100000,};
        }

        private static int cal_temperatue_r(double resistor, out double temperature)
        {
            temperature = 0;
            if (resistor < resistortable.First() || resistor > resistortable.Last())
            {
                throw new Exception($"电阻不在有效区间[{resistortable.First()},{resistortable.Last()}]");
            }

            int i;
            for (i = 0; i < resistortable.Count; i++)
            {
                if (resistor <= resistortable[i])
                {
                    break;
                }
            }
            if (resistor == resistortable[i])
            {
                temperature = temperaturetable[i];
            }
            else
            {
                temperature = temperaturetable[i - 1]
                              + (((temperaturetable[i] - temperaturetable[i - 1])
                                  * (resistor - resistortable[i - 1]))
                                 / (resistortable[i] - resistortable[i - 1]));
            }
            return 0;
        }

        private static int cal_Voltage(out long voltage, long temperature)
        {
            voltage = 0;
            if (temperature < type_e_temperature.First() || temperature > type_e_temperature.Last())
            {
                throw new Exception($"温度不在有效区间[{type_e_temperature.First()},{type_e_temperature.Last()}]");
            }

            int i;
            for (i = 0; i < type_e_temperature.Length; i++)
            {
                if (temperature <= type_e_temperature[i])
                {
                    break;
                }
            }
            if (temperature == type_e_temperature[i])
            {
                voltage = type_e_voltage[i];
            }
            else
            {
                voltage = type_e_voltage[i - 1] +
                          ((temperature - type_e_temperature[i - 1]) * (type_e_voltage[i] - type_e_voltage[i - 1]))
                          / (type_e_temperature[i] - type_e_temperature[i - 1]);
            }
            return 0;
        }

        private static int cal_temperatue(long voltage, out long temperature)
        {
            temperature = 0;

            if (voltage < type_e_voltage.First() || voltage > type_e_voltage.Last())
            {
                throw new Exception($"电压不在有效区间[{type_e_voltage.First()},{type_e_voltage.Last()}]");
            }

            int i;
            for (i = 0; i < type_e_voltage.Length; i++)
            {
                if (voltage <= type_e_voltage[i])
                {
                    break;
                }
            }
            if (voltage == type_e_voltage[i])
            {
                temperature = type_e_temperature[i];
            }
            else
            {
                temperature = type_e_temperature[i - 1]
                              + (((type_e_temperature[i] - type_e_temperature[i - 1])
                                  * (voltage - type_e_voltage[i - 1]))
                                 / (type_e_voltage[i] - type_e_voltage[i - 1]));
            }
            temperature = (((temperature) % ((long)10)) > 4 ? (((long)((temperature) / (long)10)) + 1) : ((long)((temperature) / (long)10)));
            return 0;
        }

        public static (long, long) GetLimit(double vol, double res)
        {
            long voltage = Convert.ToInt64(vol * 1000.0);
            //double extCJTemperature;
            //long extcjVol;
            //if (cal_temperatue_r(Convert.ToDouble(res), out extCJTemperature) == 0)
            //{
            //    if (cal_Voltage(out extcjVol, Convert.ToInt64(Math.Round(extCJTemperature * 100.0, 2))) == 0)
            //    {
            //        voltage = voltage + extcjVol;
            //    }
            //}
            long lowvol = voltage - 95;
            long highvol = voltage + 95;
            long temperaturelow;
            long temperaturehigh;
            cal_temperatue(lowvol, out temperaturelow);
            cal_temperatue(highvol, out temperaturehigh);
            return (temperaturelow, temperaturehigh);
        }

        #endregion 4通道电阻电压点检

        #region 1通道电阻点检

        private static int cal_temperatue(double resistor, out double temperature)
        {
            temperature = 0;
            if (resistor < resistortable.First() || resistor > resistortable.Last())
            {
                throw new Exception($"电阻不在有效区间[{resistortable.First()},{resistortable.Last()}]");
            }

            int i;
            for (i = 0; i < resistortable.Count; i++)
            {
                if (resistor <= resistortable[i])
                {
                    break;
                }
            }
            if (resistor == resistortable[i])
            {
                temperature = temperaturetable[i];
            }
            else
            {
                temperature = temperaturetable[i - 1]
                              + (((temperaturetable[i] - temperaturetable[i - 1])
                                  * (resistor - resistortable[i - 1]))
                                 / (resistortable[i] - resistortable[i - 1]));
            }
            return 0;
        }

        public static (double, double) GetLimit(double res)
        {
            double inputResistor = res;
            double resTemperature;
            int tmpres = cal_temperatue(inputResistor, out resTemperature);

            return (Math.Round(resTemperature - 3.0, 3), Math.Round(resTemperature + 3.0, 3));
        }

        #endregion 1通道电阻点检
    }
}